(function (BOM, $) {

    $(".fakeloader").fakeLoader();

    var $_Section = $('#wrapper > .section');

    $.get('html/CodeShow.html',  function () {
        $('.highlight', $_Section[0]).typewriter(arguments[0], 1).animate({
            opacity:    0.25
        }, 10000);

        var $_Title = $('.title', $_Section[0]).hide();

        setTimeout(function () {
            $_Title.fadeIn(1000);
        }, 3000);
    });
  
  var header_array = [];
  
  $('#wrapper').fullpage({
    // navigation
    anchors: ['page1', 'page2', 'page3', 'page4', 'page5', 'page6', 'page7'],
    navigation: true,

    // design
    sectionsColor: ['#ccc', '', '', '#ccc', '#ccc', '', ''],

    afterRender: function () {
      var $_this = $(this);
        
      /* save values from every header */
      $_this.find('header h4').each(function (index) {
        var $_this = $(this);

        header_array[index + 2] = $_this.html();
        $_this.html('');
      });

      /* --- initialize the state of each page --- */
      $_this.find('.section').find('.title').hide();
      $_this.find('.section3 .lang > div').hide();
      $_this.find('.section4 #canvas-wrapper .subject').hide();
      $_this.find('.section5 .content').hide();
      $_this.find('.section6 .content').hide();
      $_this.find('.section7 .content').hide();
    
      /* --- 获取屏幕宽高设置给canvas --- */
      var winWidth = $(BOM).width();

      $('#canvas').prop('width',  function () {
		if(winWidth >= 1800 ) {
			return 700;	  
		}else if (winWidth >= 850) {
          return 500;
        } else if (winWidth > 400) {
          return winWidth * 0.8;
        } else {
          return winWidth * 0.75;
        }
      });
		
	/* --- 重置下拉菜单 --- */
        var $_is_spread;

        function Select_Reset() {
            return  $(this).data('is_spread', 0).css({
                'z-index':    100,
                height:       '4em',
				'overflow-y': 'hidden'
            }).scrollTop(0);
        }

		$('.select_box ul').attr('tabindex', -1).blur(Select_Reset).data({
			value:        1,
			is_spread:    0
		}).click(function () {
			var $_this = $(this),  options_length = $('li', this).length;

			if (! $_this.data('is_spread')) {                // ready to spread
                if($_is_spread)  Select_Reset.call( $_is_spread[0] );

                $_is_spread = $_this.data('is_spread', 1).css({
                    'z-index':       10000,
                    height:          (
                        (options_length > 6)  ?  24  :  (options_length * 4)
                    ) + 'em',
                    'overflow-y':    (options_length > 6)  ?  'scroll'  :  ''
                });
            } else {                                         //ready to shrink
                if($_is_spread === $_this)  $_is_spread = null;

                var $_target = $(arguments[0].target);

                Select_Reset.call(this).prev().val( $_target.attr('value') );
                $_this.children('li:first-child').text( $_target.text() );
            }
		}).on('mousewheel touchmove', function () {
            arguments[0].stopPropagation();
        });
    },

    afterLoad: function (anchorLink, index) {
      var $_this = $(this);

      if(index == 1) {
          $_this.find('.title').fadeIn(600);
      }
      // show the title and the header
      $_this.find('header h4').typewriter(header_array[index], 70, function () {
		$_this.find('.title').fadeIn(400, function () {
          if (index == 3)
            $(this).next().find('div.lang > div').show();
        });

		if (index == 4) {
			$_this.find('canvas').fadeIn(300);
			BOM.play({
                delay:      600,
                circles:    [
                    {
                        x: 0.253, y: 0.136, radius: 0.046, color: '#39c', description: '飞扬微信公众平台 粉丝:8083 平均每日提供服务次数:214'
                    },
                    {
                        x: 0.628, y: 0.304, radius: 0.214, color: '#99c', description: '川大绩点计算器 206299次 6个版本'
                    },
                    {
                        x: 0.390, y: 0.507, radius: 0.117, color: '#669', description: '飞扬用户中心 132101次 84个版本'
                    },
                    {
                        x: 0.625, y: 0.625, radius: 0.114, color: '#069', description: '飞扬报修系统 120191次 37个版本'
                    },
                    {
                        x: 0.585, y: 0.468, radius: 0.078, color: '#39c', description: '研发实验室 66052次 3个版本'
                    },
                    {
                        x: 0.468, y: 0.390, radius: 0.097, color: '#369', description: '飞扬主页 83044次  4个版本'
                    },
                    {
                        x: 0.585, y: 0.781, radius: 0.105, color: '#99c', description: '术业有专攻 96510次 11个版本'
                    }
                ]
            },  'canvas',  function () {
				$_this.find('#canvas-wrapper .subject').fadeIn(600);
			});
		};
		  
        if (index == 6 || index == 7) {
          $_this.find('.content').fadeIn(500);
        }
      });

      if (index == 5) {
        var $_table = $_this.find('.chess > table'),
          $_td = $('<td />'),
          $_tr = $('<tr />');

        if ($_this.find('td').length === 0) {
          for (var i = 0; i < 10; i++) {
            $_tr.append($_td.clone());
          }
          for (var j = 0; j < 10; j++) {
            $_table.append($_tr.clone());
          }
        }

        $_this.find('.content').fadeIn(1000);

        function play_chess() {
          $_table.chess();
          BOM.setTimeout(play_chess, 2500);
        }

        play_chess();
      }
    },

    onLeave: function (index, nextIndex, direction) {
      var $_this = $(this);
        
      // reset the title and the header
      if(index != 1)
		$_this.find('.title').hide(300);
      $_this.find('header h4').html('');

      if (index == 3) {
        $_this.find('div.lang > div').fadeOut(100);
      }

      if (index == 4) {
        $_this.find('#canvas').fadeOut(100).parent().find('.subject').fadeOut(100);
      }

      if (index == 5) {
        $_this.find('.content').fadeOut(100);
      }

      if (index == 6 || index == 7) {
        $_this.find('.content').fadeOut(100);
      }
    }
  });

	$.fn.fullpage.setLockAnchors(true);
	
    // 表单 AJAX 提交
    
    $('#join-form').submit(function (iEvent) {
		iEvent.preventDefault();

        var $this = $(this);

        $.get(this.action + '?' + $this.serialize(),  function () {
            $.toast('提交成功', 2000, 'success');
			
			/* 重置表单 */
			$this.find('input, textarea').not('*[type="submit"]').val('');
			$this.find('.select_box ul').each(function () {
            	$(this).children('li:first-child').text(this.title);
            });
        }).fail(function () {
            $.toast({
                text:    '提交失败，请检查网络设置',
                type:    'warning'
            });
        });
    });

})(self, self.jQuery);